/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* removeNthFromEnd(struct ListNode* head, int n)
{
    int right =0;
    struct ListNode* temp = head;
    struct ListNode* prev =head;
    struct ListNode* tail = head;
    int time =0;
    while(temp)
    {
        right++;
        temp = temp->next;
    }
    if(right == 1)
    {
        head= NULL;
    }
    if(right == n && right > 1)
    {
        head = head->next;
        return head;
    }
    right =right-n;
    while(right--)
    {
        if(time >0)
        {
            prev=prev->next;
        }
        tail = tail->next;
        time++;
    }
    prev->next = tail->next;
    return head; 
}